package com.hd.manager.controller;

import com.hd.manager.shiro.token.TokenManager;
import com.hd.appcommon.entity.Privilege;
import com.hd.hdxgddcommon.controller.AbstractController;
import com.hd.hdxgddcommon.entity.dto.ResultDto;
import com.hd.hdxgddcommon.utils.StringUtils;
import com.hd.hdxgddcommon.utils.UploadToRemoteUtil;
import com.hd.hdxgddcommon.utils.tree.BaseTree;
import com.hd.hdxgddcommon.utils.tree.TreeSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.hd.appcommon.constant.PrivilegeConstant.*;

@RestController
public class IndexController extends AbstractController {

    /**
     * 当前用户左侧导航栏列表
     *
     * @return
     */
    @RequestMapping("/navigation")
    public ResultDto list() {
        Collection<Privilege> privileges = TokenManager.getTokenPrivileges();
        //菜单权限集合
        List<BaseTree> navigations = privileges.stream()
                .filter(privilege -> MENU_LEVEL_1 == privilege.getPrivilegeType() ||
                        MENU_LEVEL_2 == privilege.getPrivilegeType() ||
                        MENU_LEVEL_3 == privilege.getPrivilegeType())
                .map(Privilege::toBaseTree)
                .collect(Collectors.toList());
        return success(TreeSupport.listToTreeList(navigations));
    }

    @RequestMapping("/upload/picture")
    public ResultDto uploadPicture(@RequestParam MultipartFile picture, String directory,
                                   Integer width, Integer height) {
        if (StringUtils.isBlank(directory)) directory = "carousel";
        if (width == null || height == null) {
            String path = UploadToRemoteUtil.thumbnailsUpload(1024, 768, picture, directory);
            return path == null ? fail() : success(path);
        } else {
            String path = UploadToRemoteUtil.thumbnailsUpload(width, height, picture, directory);
            return path == null ? fail() : success(path);
        }
    }

}
